package com.yy.young.travel.service.impl;

import com.yy.young.common.util.StringUtils;
import com.yy.young.fs.service.IFileService;
import com.yy.young.travel.service.ITravelFileService;
import com.yy.young.common.util.CommonUtil;
import com.yy.young.dal.service.IDataAccessService;
import com.yy.young.dal.util.Page;
import com.yy.young.travel.model.TravelFile;
import com.yy.young.travel.util.TravelConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
 * 旅游文件服务实现
 * Created by rookie on 2018-03-23.
 */
@Service("travelFileService")
public class TravelFileServiceImpl implements ITravelFileService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource(name = "fsFileService")
    IFileService fileService;

    private static final Logger logger = LoggerFactory.getLogger(TravelFileServiceImpl.class);

    //获取数据列表
    @Override
    public List<TravelFile> getTravelFileList(TravelFile travelFile) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_FILE + ".getTravelFileList", travelFile);
    }

    @Override
    public List<TravelFile> getTravelFileBySourceIdAndType(String sourceId, String type) throws Exception {
        TravelFile travelFile = new TravelFile();
        travelFile.setSourceId(sourceId);
        travelFile.setStype(type);
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_FILE + ".getTravelFileBySourceIdAndType", travelFile);
    }

    //获取数据列表(分页)
    @Override
    public List<TravelFile> getTravelFilePage(TravelFile travelFile, Page page) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_FILE + ".getTravelFileList", travelFile, page);
    }

    //查询单条
    @Override
    public TravelFile getTravelFile(String id) throws Exception {
        return (TravelFile)dataAccessService.getObject(TravelConstants.MAPPER.TRAVEL_FILE + ".getTravelFileById", id);
    }

    //修改
    @Override
    public int updateTravelFile(TravelFile travelFile) throws Exception {
        return dataAccessService.update(TravelConstants.MAPPER.TRAVEL_FILE + ".update", travelFile);
    }

    //批量删除
    @Override
    public int deleteTravelFile(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.deleteTravelFile(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int deleteTravelFile(String id) throws Exception {
        TravelFile travelFile = this.getTravelFile(id);
        if (travelFile != null){
            dataAccessService.delete(TravelConstants.MAPPER.TRAVEL_FILE + ".delete", id);
            fileService.dropFile(travelFile.getFileId());//删除文件
            return 1;
        }
        return 0;
    }

    //插入
    @Override
    public int insertTravelFile(TravelFile travelFile) throws Exception {
        if (StringUtils.isBlank(travelFile.getId())){
            travelFile.setId(CommonUtil.getUUID());
        }
        if (StringUtils.isBlank(travelFile.getStype())){
            travelFile.setStype("1");
        }
        return dataAccessService.insert(TravelConstants.MAPPER.TRAVEL_FILE + ".insert", travelFile);
    }

}